//This will fail typecheck
const map = {};
map[someString] = "hello";
//This will pass typecheck
const map: {[key:string]: string} = {};
map[someString] = "hello";
const color : { [key: string]: any } = {
red: null,
green: null,
blue: null
};
More information on indexing in TypeScript: https://basarat.gitbook.io/typescript/type-system/index-signatures
interface IObjectKeys {
[key: string]: string | number;
}
interface IDevice extends IObjectKeys {
id: number;
room_id: number;
name: string;
type: string;
description: string;
}
const DNATranscriber = {
G: 'C',
C: 'G',
T: 'A',
A: 'U'
};
export default class Transcriptor {
toRna(dna: string) {
const codons = [...dna];
if (!isValidSequence(codons)) {
throw Error('invalid sequence');
}
const transcribedRNA = codons.map(codon => DNATranscriber[codon]);
return transcribedRNA;
}
}
function isValidSequence(values: string[]): values is Array<keyof typeof DNATranscriber> {
return values.every(isValidCodon);
}
function isValidCodon(value: string): value is keyof typeof DNATranscriber {
return value in DNATranscriber;
}